/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager.createflights;

import assignment_cp3120.dataObjects.AllLocations;
import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCCreateFlight;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrator
 */
public class CreateFlightsView extends JPanel {  
    private JButton createFlightBtn;
    private CreateFlightsModel model;
    private GridLayout layout;
    private JPanel depPanel;
    private JPanel destPanel;
    private JPanel depD_TPanel;
    private JPanel capacityPanel;
    private JPanel flightPricePanel;
    private JPanel buttonPanel;

    private JComboBox depCBox;
    private JLabel depLabel;            

    private JComboBox destCBox;
    private JLabel destLabel;           

    private JTextField priceField;

    private JLabel priceLabel;

    private JPanel flightContainer;

    private JComboBox dayCBox;
    private JComboBox monthCBox;
    private JComboBox yearCBox;
    private JComboBox dhoursCBox;
    private JComboBox dminutesCBox;
    private JComboBox ahoursCBox;
    private JComboBox aminutesCBox;

    private JPanel dateContainer;
    private JPanel depTimeContainer;
    private JPanel arrTPanel;
    private JPanel arrTimePanel;

    private JTextField capacityField;
    private JLabel capacityLabel;
    private JPanel capacityContainer;
    
    public CreateFlightsView() {     
        this(new CreateFlightsModel());
    }
    public CreateFlightsView(CreateFlightsModel model) {
        this.model = model;        
        createPanel(); 
    }
    private void createPanel(){
        layout = new GridLayout(0, 2);
        depPanel = new JPanel();
        destPanel = new JPanel();
        depD_TPanel = new JPanel();
        capacityPanel = new JPanel();
        flightPricePanel = new JPanel();
        buttonPanel = new JPanel();
        createFlightBtn = new JButton("Create");
        
        createFlightBtn.addActionListener(new UC2AL(new UCCreateFlight(model, this)));

        depCBox = new JComboBox(AllLocations.getLocationsNames());
        depLabel = new JLabel("Departure Location:");            

        depPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        depPanel.add(depLabel);
        depPanel.add(depCBox);
        
        
        destCBox = new JComboBox(AllLocations.getLocationsNames());
        destLabel = new JLabel("Destination Location:");            

        destPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        destPanel.add(destLabel);
        destPanel.add(destCBox);

        priceField = new JTextField("mmmmmmmmmm");
        priceField.setPreferredSize(priceField.getPreferredSize());
        priceField.setText("");
        priceLabel = new JLabel("Flight Price $");

        flightPricePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        flightPricePanel.add(priceLabel);
        flightPricePanel.add(priceField);

        flightContainer = new JPanel(new GridLayout(0, 1));
        flightContainer.add(new JPanel());
        flightContainer.add(flightPricePanel);

        String[] dayArray;
        String[] monthArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        String[] yearArray;
        
        int baseYear = Calendar.getInstance().get(Calendar.YEAR);
        
        int years = baseYear + 50;
        int days = 31;

        yearArray = new String[years];
        dayArray = new String[days];
        
        for (int i= 0; i < years; i++){
            yearArray[i] = Integer.toString(baseYear + i);
        }        
        for (int i= 1; i <= days; i++){
            dayArray[i-1] = Integer.toString(i);
        }
        for (int i = 0; i < 24; i++){
        }
        String[] hours = new String[24];
        String[] minutes = new String[60];
        for (int i=0; i < hours.length; i++){
            if (i<9){
                hours[i] = "0" + Integer.toString(i+1);
            }
            else {
                hours[i] = Integer.toString(i+1);
            }                
        }
        for (int i=0; i < minutes.length; i++){
            if (i<9){
                minutes[i] = "0" + Integer.toString(i);
            }
            else {
                minutes[i] = Integer.toString(i);
            }
        }            

        dayCBox = new JComboBox(dayArray);
        monthCBox = new JComboBox(monthArray);
        yearCBox = new JComboBox(yearArray);
        dhoursCBox = new JComboBox(hours);
        dminutesCBox = new JComboBox(minutes);
        ahoursCBox = new JComboBox(hours);
        aminutesCBox = new JComboBox(minutes);

        depD_TPanel.setLayout(new GridLayout(0,1));
        dateContainer = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        depTimeContainer = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        arrTPanel = new JPanel(new GridLayout(0,1));
        arrTimePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        dateContainer.add(new JLabel("Dep Date:"));

        dateContainer.add(dayCBox);
        dateContainer.add(monthCBox);
        dateContainer.add(yearCBox);
        depTimeContainer.add(new JLabel("Dep Time:"));
        depTimeContainer.add(dhoursCBox);
        depTimeContainer.add(new JLabel(":"));
        depTimeContainer.add(dminutesCBox);

        depD_TPanel.add(dateContainer);
        depD_TPanel.add(depTimeContainer);

        arrTimePanel.add(new JLabel("Arr Time:"));
        arrTimePanel.add(ahoursCBox);
        arrTimePanel.add(new JLabel(":"));
        arrTimePanel.add(aminutesCBox);

        arrTPanel.add(new JPanel());
        arrTPanel.add(arrTimePanel);

        capacityField = new JTextField("mmmmmmmmmm");
        capacityField.setPreferredSize(priceField.getPreferredSize());
        capacityField.setText("");
        capacityLabel = new JLabel("Plane Capacity");

        capacityPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        capacityPanel.add(capacityLabel);
        capacityPanel.add(capacityField);

        capacityContainer = new JPanel(new GridLayout(0, 1));
        capacityContainer.add(new JPanel());
        capacityContainer.add(capacityPanel);

        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(createFlightBtn);
   
        setLayout(layout);

        add(depPanel);
        add(destPanel);            
        add(depD_TPanel);
        add(arrTPanel);
        add(capacityContainer);
        add(flightContainer);
        add(buttonPanel);
    }
    public void clearView() {
        this.removeAll();
        createPanel();
        this.validate();
    }
    public String getDepName(){
        return (String)depCBox.getSelectedItem();
    }
    public String getDestName(){
        return (String)destCBox.getSelectedItem();
    }
    public String getDate(){
        return yearCBox.getSelectedItem()
                    +"-"+Integer.toString(monthCBox.getSelectedIndex()+1)
                    +"-"+dayCBox.getSelectedItem().toString();
    }
    public String getArrTime(){
        return ahoursCBox.getSelectedItem() + ":" 
                + aminutesCBox.getSelectedItem() + ":00";
    }
    public String getDepTime(){
        return dhoursCBox.getSelectedItem() + ":" 
                + dminutesCBox.getSelectedItem() + ":00";
    }
    public int getCapacity(){
        return Integer.parseInt(capacityField.getText());
    }
    public int getPrice(){
        return Integer.parseInt(priceField.getText());
    }
}
